package Polymorphism4;
// 回顾一下：数组是怎么排序的

import java.util.Arrays;
import java.util.Comparator;

class Student{
    public int age;
    public String name;
    public double score;

    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
//    @Override
//    public int compareTo(Student o){
////        // 这里 this.age  谁调用compareTo，谁就是 this所指的对象
////        if(this.age>o.age){
////            return 1;
////        }else if(this.age==o.age){
////            return 0;
////        }
////        return -1;
////    return this.age - o.age;
//      //  return o.age - this.age;
//        // 原来是 this.gae 比 o.age 大，交换位置，等价于this.age往前移
//        //  现在 o.age 比 this.age大，交换位置，等价于 o.age 向前移
//
//        return this.name.compareTo(o.name);
//    }
}

class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}
class ScoreComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1,Student o2){
        return  (int)(o1.score - o2.score);
    }
}
class NameComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(18, "z", 80.5);
        students[1] = new Student(16, "a", 96.8);
        students[2] = new Student(19, "c", 81.5);
        System.out.println(Arrays.toString(students));
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,nameComparator);
        System.out.println(Arrays.toString(students));

        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students,ageComparator);// 再给它传个参数
        System.out.println(Arrays.toString(students));

        ScoreComparator scoreComparator = new ScoreComparator();
        Arrays.sort(students,scoreComparator);
        System.out.println(Arrays.toString(students));
    }



//    public static void main(String[] args) {
//        //假设现在有两个学生
//        Student student = new Student(18,"小明",80.5);
//        Student student2 = new Student(16,"小红",96.8);
//        AgeComparator ageComparator = new AgeComparator();
//        System.out.println(ageComparator.compare(student,student2));
//
//
//        // if(student > student2) 这样去写是错误的，因为在java中，是无法拿到地址的
//        // 我们上面写一个比较方法
////        if(student.compareTo(student2)>0){
////            // 意思就是 student 与 student2  按照 compareTo 的规则去比较
////        }
////        System.out.println(student.compareTo(student2));
//    }



    public static void main1(String[] args) {
        int[] array = {1,21,3,14,5,16};
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
